package com.runfast.paotui.mq;

import cn.jpush.api.push.PushResult;
import com.alibaba.fastjson.JSON;
import com.runfast.common.exception.BaseException;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.paotui.dao.model.*;
import com.runfast.paotui.service.RunfastShopperService;
import com.runfast.paotui.dao.model.RunfastAgentbusiness;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.util.JiGuangUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sun.java2d.pipe.SpanShapeRenderer;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author: lijin
 * @date: 2018年04月18日
 */

@Slf4j
@Component
public class AmqpClient {
    
    @Autowired
    private RunfastShopperService runfastShopperService;
    
    
    private final AmqpAdmin amqpAdmin;
    private final AmqpTemplate amqpTemplate;
    @Autowired
    public AmqpClient(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate) {
        this.amqpAdmin = amqpAdmin;
        this.amqpTemplate = amqpTemplate;
    }

//    @Scheduled(fixedDelay = 10000)
    public void publish(){
        MessageProperties messageProperties = new MessageProperties();

        messageProperties.setType("order");
        SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();
        Message message = simpleMessageConverter.toMessage(new Order(), messageProperties);
        amqpTemplate.send("order",message);

    }


    @RabbitListener(queues = "order" )
    public void processMessage1(Message message) {
        MessageProperties messageProperties = message.getMessageProperties();
        SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();
        Order order = (Order) simpleMessageConverter.fromMessage(message);

        log.info("1. type: "+messageProperties.getType()+", body: "+order.toString());

    }

    @RabbitListener(queues = "order" )
    public void processMessage2(Message message) {
        MessageProperties messageProperties = message.getMessageProperties();
        SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();
        Order order = (Order) simpleMessageConverter.fromMessage(message);

        log.info("2. type: "+messageProperties.getType()+", body: "+order.toString());

    }


    @RabbitListener(queues = "order_model" )
    public void processMessage3(Message message){
        MessageProperties messageProperties = message.getMessageProperties();
        SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();
        //Order order = (Order) simpleMessageConverter.fromMessage(message);


        RunfastAgentbusiness runfastAgentbusiness = JSON.parseObject(new String(message.getBody()), RunfastAgentbusiness.class);


        //RunfastAgentbusiness runfastAgentbusiness=( RunfastAgentbusiness)JSON.parseObject(new String(message.getBody()));
        RunfastShopperExample runfastShopperExample=new RunfastShopperExample();
        runfastShopperExample.createCriteria().andAgentIdEqualTo(runfastAgentbusiness.getId()).andWorkingEqualTo(1);
        List<RunfastShopper> sh = runfastShopperService.selectByExample(runfastShopperExample);
        HashMap<String,String> params = new HashMap<>();

        //按照目前有登录记录和是开工状态
        List<RunfastShopper> sh2=runfastShopperService.getshopper(runfastAgentbusiness.getId());
        //System.out.println("="+sh2);

        if(sh.size()>0) {

            //true为已在另一端重复登录，需要通知上一个登录的用户重新登陆
            ArrayList<String> aliases = new ArrayList();
            //aliases.add(runfastLoginRecord.get(0).getAlias());//（查看上一个登录状态,之前可能存在多个）
            for (RunfastShopper r : sh2) {
                if(r.getAlias()!=null){
                    aliases.add(r.getAlias());
                }
                //System.out.println(r);
            }
            if(aliases.size()>0){

                try {
                    if(runfastAgentbusiness.getOrderMode()!=null && runfastAgentbusiness.getOrderMode()==2){
                        params.put("waimai_mode", "1");//改为派单
                        PushResult pushResult = JiGuangUtil.push(Role.Predefine.DRIVER, 600, aliases, "代理商开启了派单模式","派单模式", params, "");

                    }else {
                        params.put("waimai_mode", "2");//改为抢单
                        PushResult pushResult = JiGuangUtil.push(Role.Predefine.DRIVER, 600, aliases, "代理商开启了抢单模式","抢单模式", params, "");
                    }
                }catch (Exception e){
                    log.info("waimai_mode. type: "+messageProperties.getType()+", body: "+runfastAgentbusiness.toString());
                }



            }
        }


        //System.out.println(runfastAgentbusiness);
        log.info("3. type: "+messageProperties.getType()+new String(message.getBody()));
    }


}
